package api

import (
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql/db_main"

	"github.com/jinzhu/copier"
	"github.com/labstack/echo/v4"
)

// @Summary 获取协议列表
// @Description 获取内容列表
// @Accept  json
// @Produce  json
// @Tags Protocol
// @Success 200 {object} api_models.GetProtocolListResp "result"
// @Router /api/protocol/getProtocolList [POST]
func (base *Controller) GetProtocolList(c echo.Context) error {
	//protocolList, err := db_main.GetProtocolList()
	//resp := make([]*api_models.GetProtocolListResp, 0)
	//for _, protocol := range protocolList {
	//	des := &api_models.GetProtocolListResp{}
	//	err := copier.Copy(des, protocol)
	//	if err != nil {
	//		return util.BuildFail(c, 1, "", err)
	//	}
	//	runes := []rune(util.TrimHtml(protocol.Content))
	//	i := 50
	//	if len(runes) < 50 {
	//		i = len(runes)
	//	}
	//	des.Profile = string(runes[0:i])
	//	resp = append(resp, des)
	//}
	//if err != nil {
	//	return util.BuildFail(c, 1, "", err)
	//}
	//if len(resp) == 0 {
	return util.BuildSuccess(c, []interface{}{}, "")
	//}
	//return util.BuildSuccess(c, resp, "")
}

// @Summary 根据ID获取协议
// @Description 根据ID获取协议
// @Accept  json
// @Produce  json
// @Tags Protocol
// @Param   sendBody body api_models.GetProtocolByIdReq true "body"
// @Success 200 {object} api_models.GetProtocolByIdResp "result"
// @Router /api/protocol/getProtocolById [POST]
func (base *Controller) GetProtocolById(c echo.Context) error {
	r := &api_models.GetProtocolByIdReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BadParam(c, err)
	}
	protocol, err := db_main.GetProtocol(r)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	resp := &api_models.GetProtocolByIdResp{}
	err = copier.Copy(resp, protocol)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}
